﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using WmsApp.Report;
using WmsApp.Security;
using WmsApp.StaticData;
using WmsApp.StaticData.Unity;
using WmsApp.Window;
using WmsApp.WindowControl;
using WmsApp.WmsService;

namespace WmsApp.UI
{
    public partial class PayApplyBillCtl : DevExpress.XtraEditors.XtraUserControl
    {
        private PayApplyBillController controller = null;
        public PayApplyBillCtl()
        {
            InitializeComponent();
            if (GlobalClass.RunTimeMode)
            {
                controller = new PayApplyBillController(this);
            }
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            if (GlobalClass.RunTimeMode)
            {
                SubjectFactory.AddObserverToSubject(new CustomerObserver("customer", SubjectFactory.GetSubject(),
                                                                         this.lookUpEditProvider3.Properties,
                                                                         (ICanClosing) this.FindForm()));
            }
        }

        public PayApplyBillController FPayApplyBillController
        {
            get { return controller; }
        }

        private void CmdNewDtl3_Click(object sender, EventArgs e)
        {
            FPayApplyBillController.AddPayApplyBillDtl();
        }

        private void CmdSave3_Click(object sender, EventArgs e)
        {
            FPayApplyBillController.SavePayApplyBill();
        }

        private void CmdInAffirm3_Click(object sender, EventArgs e)
        {
            FPayApplyBillController.AffirmInStoreBill();
        }

        private void CmdDelete_Click(object sender, EventArgs e)
        {
            var dtl = (PayApplyBillDtl)this.gridView3.GetRow(this.gridView3.FocusedRowHandle);
            if (dtl != null)
            {
                FPayApplyBillController.RemovePayApplyBillDtl(dtl);
            }
        }

        private void CmdNew3_Click(object sender, EventArgs e)
        {
           FPayApplyBillController.NewPayApplyBill(String.Empty);
        }

        private void CmdAddPayItem_Click(object sender, EventArgs e)
        {
            if(FPayApplyBillController != null &&
                ! String.IsNullOrEmpty(FPayApplyBillController.CurrentPayApplyBillID))
            {
                string payApplyBillItemID = FormPayApplyBillItemEditor.ShowForm(FPayApplyBillController.CurrentPayApplyBillID);
                if(!String.IsNullOrEmpty(payApplyBillItemID))
                {
                    //执行刷新动作
                    //TODO
                    FPayApplyBillController.LoadPayApplyBillID(FPayApplyBillController.CurrentPayApplyBillID);
                }
            }
            else
            {
                XtraMessageBox.Show("当前编辑器没有任何付款单！", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void RepositoryItemButtonEditCancelPayButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            var obj = this.gridView1.GetFocusedRow();
            if (obj != null)
            {
                try
                {
                    var client = Factory.WmsServiceFactory.GetServiceClient();
                    if (System.Windows.Forms.DialogResult.Yes == XtraMessageBox.Show("您确实要取消该付款单吗？", "提问", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
                    {
                        var currentObj = (PayApplyBillItem)obj;
                        client.CancelPayApplyBillItem(currentObj.PayApplyBillItemID);
                        //后台实际对象
                        var backStorageObj = client.GetPayApplyBillItem(currentObj.PayApplyBillItemID);
                        //赋值，刷新
                        currentObj.ItemState = backStorageObj.ItemState;
                        currentObj.CancelID = backStorageObj.CancelID;
                        currentObj.CancelItemDate = backStorageObj.CancelItemDate;
                        currentObj.CancelName = backStorageObj.CancelName;
                        this.gridControlPayItem.RefreshDataSource();
                    }
                }
                catch (Exception ex)
                {
                    Exceptions.ExceptionHandler.ShowException(ex);
                }
            }
        }

        private void CmdPrint_Click(object sender, EventArgs e)
        {
            if (FPayApplyBillController != null && !String.IsNullOrEmpty(FPayApplyBillController.CurrentPayApplyBillID))
            {
                if (!String.IsNullOrEmpty(FPayApplyBillController.CurrentPayApplyBillID))
                {
                    IReport report = new ReportPayApplyBill();
                    report.ShowReport(FPayApplyBillController.CurrentPayApplyBillID);
                }
            }
            else
            {
                XtraMessageBox.Show("当前编辑器没有任何付款单！", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void CmdAllPay_Click(object sender, EventArgs e)
        {
            try
            {
                if (FPayApplyBillController != null && !String.IsNullOrEmpty(FPayApplyBillController.CurrentPayApplyBillID))
                {
                    IConfirmPayWrap confirm = new ConfirmPayWrap();
                    if ( confirm.IsConfirmOk)
                    {
                        var client = Factory.WmsServiceFactory.GetServiceClient();
                        var item = new PayApplyBillItem
                        {
                            PayApplyBillID = FPayApplyBillController.CurrentPayApplyBillID,
                            PayID = UserSession.CurrentUser.USERID,
                            ItemState = "正常",
                            PayTypeCode = confirm.PayTypeCode,
                            PayTypeName = confirm.PayTypeName,
                            //付款项时间为确认框中的最后付款时间
                            PayTime = confirm.ConfirmDate,
                            PayName = UserSession.CurrentUser.USERNAME,
                            PayAmount = FPayApplyBillController.CurrentPayApplyBillTotalAmount
                        };
                        client.NewPayApplyBillItem(item);

                        //执行刷新动作
                        FPayApplyBillController.LoadPayApplyBillID(FPayApplyBillController.CurrentPayApplyBillID);
                    }
                }
            }
            catch(Exception exception)
            {
                Exceptions.ExceptionHandler.ShowException(exception);
            }
        }
    }
}
